<?php
/* 
 * ********* CVS HEADER ********* 
 * 
 * $RCSfile: admpluginstall.php,v $ 
 * $Date: 2005/08/29 15:21:09 $ 
 * $Revision: 1.2 $ 
 * 
 * ********* CVS HEADER ********* 
 */
require_once("module/TPLN/TPLN.php");
require_once("module/INI/readinifile.php");
if($plugin==''){
	header("Location: ?op=admplugliste");
	exit;	
}
function split_sql($sql)
{
    $sql = trim($sql);
    $buffer = array();
    $ret = array();
    $in_string = false;

    for($i=0; $i<strlen($sql); $i++)
    {
         if($sql[$i] == ";" && !$in_string)
        {
            $ret[] = substr($sql, 0, $i);
            $sql = substr($sql, $i + 1);
            $i = 0; 
        }

        if($in_string && ($sql[$i] == $in_string) && $buffer[1] != "\\")
        {
             $in_string = false;
        }
        elseif(!$in_string && ($sql[$i] == "\"" || $sql[$i] == "'") && $buffer[0] != "\\")
        {
             $in_string = $sql[$i];
        }
        if(isset($buffer[1]))
        {
            $buffer[0] = $buffer[1];
        }
        $buffer[1] = $sql[$i];
     }    
    
    if (!empty($sql))
    {
        $ret[] = $sql;
    }

    return($ret);
}
function launch_sql($file) {
	if(!empty($file))
	{
	    $sql_query = fread(fopen($file, "r"), filesize($file));
	    $pieces  = split_sql($sql_query);
	    for ($ii=0; $ii<count($pieces); $ii++)
	    {
	    $pieces[$ii] = trim($pieces[$ii]);
	    $result = rportalrequete ($pieces[$ii]) or rportalerreur($pieces[$ii],"?op=admplugliste");
	    
	    }
	}
}
$query="SELECT * FROM rportal_installation WHERE directory='$plugin'";
$result=rportalrequete($query) or rportalerreur($query,"?op=");
if(rportalnbresult($result)==0){
	$fichier = "plugins/$plugin/param/plugin.ini";
	if(file_exists($fichier)){
		$plugparams = new readinifile();
		$plugparams->readfile($fichier);
		/**
		  * 0) on cree le fichier back.ini qui servira a definir la config avant l'install
		  *    et on determine s'il s'agit d'une installation ou d'une mise a jour
		  */
		if(file_exists("plugins/$plugin/param/back.ini"))
		unlink("plugins/$plugin/param/back.ini");
		touch("plugins/$plugin/param/back.ini");
		$fichier_install = new readinifile();
		$fichier_install->readfile("plugins/$plugin/param/back.ini");
		
		$update = 0;
		if($rportalparams->key_exists($plugparams->get_value("INSTALL","plugin.section"),"sn")){
			if($rportalparams->get_value($plugparams->get_value("INSTALL","plugin.section"),"sn")
			== ($plugparams->get_value("INSTALL","plugin.version.sn")-1)){
				$update = 1;
			}else{
				rportalerreur("Vous essayez de faire une mise a jour avec le mauvais plugin, veuillez vous procurez le plugin avec le SN=".($rportalparams->get_value($plugparams->get_value("INSTALL","plugin.section"),"sn")+1),"?op=");			
			}
		}
		/**
		  * 0.1) On verifie les dependances
		  */
		if($plugparams->section_exists("DEPENDANCE")){
			$section = $plugparams->get_section("DEPENDANCE");
			if(count($section)!=0){
				foreach ( $section as $cle=>$valeur ){
					if(false == $rportalparams->section_exists($cle)){
						rportalerreur("Votre plugin requiere le plugin suivant pour s'installer : $cle avec SN=$valeur ou superieur.","?op=");
					}
					if($valeur > $rportalparams->get_value($cle,"plugin.version.sn")){
						rportalerreur("Votre plugin requiere le plugin suivant pour s'installer : $cle avec SN=$valeur ou superieur.\n<br/> Une version anterieure est installee, faites une mise a jour.","?op=");
					}
				}
			}
		}
		/**
		  * 1) on execute le script definit par plugin.script.initialisation
		  */
		$p = trim($plugparams->get_value("INSTALL","plugin.script.initialisation"));
		if($p!='' && file_exists("plugins/$plugin/$p")) include("plugins/$plugin/$p");
		/**
		  * 2) on execute les requetes SQL contenu dans les fichiers
		  * plugin.sql.table puis plugin.sql.data
		  */
		if($update==0){
			$p = trim($plugparams->get_value("INSTALL","plugin.sql.table"));
			if($p!='' && file_exists("plugins/$plugin/$p")) launch_sql("plugins/$plugin/$p");
			$p = trim($plugparams->get_value("INSTALL","plugin.sql.data"));
			if($p!='' && file_exists("plugins/$plugin/$p")) launch_sql("plugins/$plugin/$p");
		}else{
			$p = trim($plugparams->get_value("INSTALL","plugin.sql.update.table"));
			if($p!='' && file_exists("plugins/$plugin/$p")) launch_sql("plugins/$plugin/$p");
			$p = trim($plugparams->get_value("INSTALL","plugin.sql.update.data"));
			if($p!='' && file_exists("plugins/$plugin/$p")) launch_sql("plugins/$plugin/$p");	
		}
		/**
		  * 3) On cree les menus definis par plugin.script.menu
		  */
		$p = trim($plugparams->get_value("INSTALL","plugin.script.menu"));
		if($p!='' && file_exists("plugins/$plugin/$p")) include("plugins/$plugin/$p");
		/**
		  * 4) On cree les fonctions definies par plugin.script.fonction
		  */
		$p = trim($plugparams->get_value("INSTALL","plugin.script.fonction"));
		if($p!='' && file_exists("plugins/$plugin/$p")) include("plugins/$plugin/$p");
		/**
		  * 5) On cree les boxs definis dans la section [BOX]
		  */
		if($plugparams->section_exists("BOX")){
			$boxs = $plugparams->get_section("BOX");
			foreach ( $boxs as $cle=>$valeur ){
				$rportalparams->drop_key("BOX",$cle);
				$rportalparams->add_key("BOX",$cle,$valeur);
			}	
		}
		/**
		  * 6) Mise a jour des modules TPLN et readinfile
		  */
		if(file_exists("plugins/$plugin/module/INI/readinifile.php"))
			if(!copy("plugins/$plugin/module/INI/readinifile.php","module/INI/readinifile.php"))
			rportalerreur("Le fichier: plugins/$plugin/module/INI/readinifile.php n'a pas pu etre copie","?op=admplugliste");
		
		if(is_dir("plugins/$plugin/module/TPLN")){
			$handle=opendir("plugins/$plugin/module/TPLN");
			while($f=readdir($handle)) {
				if(($f!=".")&&($f!=".."))
				{
					if(is_file("plugins/$plugin/module/TPLN/$f"))
					{
						if(!copy("plugins/$plugin/module/TPLN/$f","module/TPLN/$f"))
						rportalerreur("Le fichier: plugins/$plugin/module/TPLN/$f n'a pas pu etre copie","?op=admplugliste");
					}
				}
			}
		}
		/**
		  * 7) On cree les modules definies dans la section [MODULES]
		  */
		if($plugparams->section_exists("MODULES")){
			$modules = $plugparams->get_section("MODULES");
			foreach ( $modules as $cle=>$valeur ){
				$rportalparams->drop_key("MODULES",$cle);
				$rportalparams->add_key("MODULES",$cle,$valeur);
			}	
		}
		/**
		 * 8) On copie les repertoires definis dans la section [REPERTOIRES]
		 */
		if($plugparams->section_exists("REPERTOIRES")){
			$reps = $plugparams->get_section("REPERTOIRES");
			foreach ( $reps as $from=>$mixed ){
				$t = explode(",",$mixed);
				$to = $t[0];
				if(false!==strpos($to,'/') && strpos($to,'/')==0){
					$to = substr($to, 1);
				}
				$option = $t[1]; //u: for update, c: for creation only, n: for a copy without overwriting
				if($option == ''){
					$option = 'n';	
				}
				if($option=='c'){
					if(false==is_dir($to)){
						mkdir ($to, 0777);
					}
				}elseif($option=='u' || $option=='n'){
					// debut de la boucle de copie
					if(is_dir("plugins/$plugin/$from/")){
						$repori = "plugins/$plugin/$from/";
						$repdest = $to;
						if(false==is_dir($repdest)){
							mkdir ($repdest, 0777);
						}
						$repdest.='/';
						$handle = opendir($repori);
						while($f=readdir($handle)) {
							if(($f!=".")&&($f!=".."))
							{
								if((is_file($repdest.$f)&&$option=='u')||false===is_file($repdest.$f)){
									if(is_file($repori.$f))
									{
										copy($repori.$f,$repdest.$f);
									}
								}
							}
						}
					}
					//Fin de la boucle de copie
				}
				
			}	
		}
		 
		/**
		  * 9) On copie les images dans le repertoire images/<plugin.images.repertoire>/
		  */
		if(is_dir("plugins/$plugin/images")){
			$repori = "plugins/$plugin/images/";
			$repdest = $plugparams->get_value("INSTALL","plugin.images.repertoire");
			if($repdest!=''){
				$repdest = 'images/'.$repdest.'/';	
			}else{
				$repdest = 'images/';	
			}
			if(false==is_dir($repdest)){
				mkdir ($repdest, 0777);
			}
			$handle = opendir($repori);
			while($f=readdir($handle)) {
				if(($f!=".")&&($f!="..")&&($f!=".htaccess"))
				{
					if(is_file($repori.$f))
					{
						copy($repori.$f,$repdest.$f);
					}
				}
			}
		}
		/**
		  * 10) on cree les sequences grace au script definit par plugin.script.sequence
		  */
		$p = trim($plugparams->get_value("INSTALL","plugin.script.sequence"));
		if($p!='' && file_exists("plugins/$plugin/$p")) include("plugins/$plugin/$p");
		/**
		  * 11) on execute le script definit par plugin.script.finalisation
		  */
		$p = trim($plugparams->get_value("INSTALL","plugin.script.finalisation"));
		if($p!='' && file_exists("plugins/$plugin/$p")) include("plugins/$plugin/$p");
		
		/**
		  * 12) On met a jour le fichier paramsite.ini
		  */
		//Section
		$s = trim($plugparams->get_value("INSTALL","plugin.section"));
		if(!$rportalparams->section_exists($s)) $rportalparams->add_section($s);
		//Maj nom, version, sn
		$nom = trim($plugparams->get_value("INSTALL","plugin.nom"));
		$version = trim($plugparams->get_value("INSTALL","plugin.version.nom"));
		$sn = trim($plugparams->get_value("INSTALL","plugin.version.sn"));
		$rportalparams->drop_key($s,"nom");
		$rportalparams->add_key($s,"nom",$nom,$type="string");
		$rportalparams->drop_key($s,"version");
		$rportalparams->add_key($s,"version",$version,$type="string");
		$rportalparams->drop_key($s,"sn");
		$rportalparams->add_key($s,"sn",$sn,$type="string");
		// On cree les cles dans la section du plugin qui n'existe pas deja
		// Attention pas de mise a jour
		if($plugparams->section_exists($s)){
			$section = $plugparams->get_section($s);
			foreach ( $section as $cle=>$valeur ){
				if(!$rportalparams->key_exists($s,$cle)) $rportalparams->add_key($s,$cle,$valeur);					
			}
		}
		/**
		  * 13) On stocke dans la BDD l'installation
		  */
		$p = trim($plugparams->get_value("INSTALL","plugin.nom"));
		$P.= " : ".trim($plugparams->get_value("INSTALL","plugin.version"));
		$query="INSERT INTO rportal_installation (id,directory,titre,date,uninstall)VALUES ('','$plugin','".addslashes($p)."',NOW(),'1')";
		$result = rportalrequete ($query) or rportalerreur($query,"?op=admplugliste");
				  
		
	}
}
header("Location: ?op=admplugliste");
exit;
?>